package nl.fonfolio.view.createavatar {
	import flash.events.Event;	
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.events.MouseEvent;	

	/**
	 * @author lindy
	 */
	public class CreateAvatar extends Sprite {
		public var step1_btn : MovieClip;
		public var step2_btn : MovieClip;
		public var step3_btn : MovieClip;
		public var step4_btn : MovieClip;
		public var step5_btn : MovieClip;
		public var next_btn : MovieClip;
		public var previous_btn : MovieClip;
		public var reset_btn : MovieClip;
		public var save_btn : MovieClip;
		public var random_btn : MovieClip;
		public var avatarstep_lbl : TextField;
		public var avatarstep_txt : TextField;
		public var step_label : TextField;
		public var step_txt : TextField;
		private var avatarElement : Sprite;
		private var currentStep : Number;
		private var totalSteps:Number = 5;

		public function CreateAvatar() {
			this.step1_btn.addEventListener(MouseEvent.CLICK, openProfile);
			this.step2_btn.addEventListener(MouseEvent.CLICK, openEye);
			this.step3_btn.addEventListener(MouseEvent.CLICK, openNose);
			this.step4_btn.addEventListener(MouseEvent.CLICK, openMouth);
			this.step5_btn.addEventListener(MouseEvent.CLICK, openFace);
			this.next_btn.addEventListener(MouseEvent.CLICK, nextStep);
			this.previous_btn.addEventListener(MouseEvent.CLICK, previousStep);
			this.setStep(1);
		}
		
		private function previousStep(event : MouseEvent) : void {
			if (this.currentStep > 1){
				this.currentStep --;
				setStep(this.currentStep);
			}
		}

		private function nextStep(event : MouseEvent) : void {
			if (this.currentStep < this.totalSteps){
				this.currentStep ++;
				setStep(this.currentStep);
			}
		}

		private function openProfile(event : MouseEvent) : void {
			setStep(1);
		}

		private function openEye(event : MouseEvent) : void {
			setStep(2);
		}

		private function openNose(event : MouseEvent) : void {
			setStep(3);
		}

		private function openMouth(event : MouseEvent) : void {
			setStep(4);
		}

		private function openFace(event : MouseEvent) : void {
			setStep(5);
		}

		private function loadElement(sort : String, scale : Number) : void {
			if (this.avatarElement) {
				removeChild(this.avatarElement);
			}
			this.avatarElement = new AvatarElement(sort, scale);
			this.avatarElement.y = 90;
			addChild(this.avatarElement);
		}

		private function setStep(step:Number) : void {
			this.currentStep = step;
			this.setStepText(step);
			switch(step){
				case 1:
					if (this.avatarElement) {
						removeChild(this.avatarElement);
					}
					this.avatarElement = new AvatarProfile();
					this.avatarElement.y = 90;
					addChild(this.avatarElement);
					break;	
				case 2:
					loadElement("eye", 3);
					break;	
				case 3:
					loadElement("nose", 6);
					break;	
				case 4:
					loadElement("mouth", 1.5);
					break;	
				case 5:
					loadElement("face", 0.75);
					break;	
			}
		}

		private function setStepText(step:Number) : void {
			this.step_txt.text = step+"/"+totalSteps;
			this.avatarstep_txt.text = step+"/"+totalSteps;
		}
	}
}
